﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;

namespace ESHOP.DAO
{
    public class SellLeadDao: BaseDAO
    {
        public List<SELL> GetAllSellByCompanyId(string companyId)
        {
            return _db.SELLs.Where(p => p.MACONGTY.Equals(companyId)&&p.ISHOW==true).ToList();
        }
        public List<SELL> SearchSell(String Keyword, String danhmuc, String xuatxu)
        {
            var Str = _db.SELLs.ToList();
            if (!String.IsNullOrEmpty(Keyword))
            {
                Str = Str.Where(p =>!String.IsNullOrEmpty(p.SUBJECT)&& p.SUBJECT.Contains(Keyword) || p.KEYWORD.Contains(Keyword)).ToList();
            }
            if (!String.IsNullOrEmpty(danhmuc) && !danhmuc.Equals("%"))
            {
                Str = Str.Where(P => P.CATAGORY_ID.Equals(danhmuc)).ToList();
            }
            if (!String.IsNullOrEmpty(xuatxu))
            {
                Str = Str.Where(p => p.MAQUOCGIA.Equals(xuatxu)).ToList();
            }
            Str = Str.Where(p => p.ISHOW.Equals(true)).ToList();
            return Str.OrderByDescending(p => p.ID).ToList();
        }
        public int AddNew(SELL sellObject)
        {
            try
            {
                _db.SELLs.InsertOnSubmit(sellObject);
                _db.SubmitChanges();
                return 1;
            }
            catch
            {

                return 0;
            }
            
        }
        public string NewId()
        {
            string sReturn = "";

            string sToday = "";
            sToday = DateTime.Now.Year.ToString();
            sToday = DateTime.Now.Month < 10
                         ? sToday + "0" + DateTime.Now.Month.ToString()
                         : sToday + DateTime.Now.Month.ToString();

            sToday = DateTime.Now.Day < 10
                         ? sToday + "0" + DateTime.Now.Day.ToString()
                         : sToday + DateTime.Now.Day.ToString();

            sReturn = (from p in _db.SELLs.Where(p => p.ID.Substring(0, 8).Contains(sToday))
                       select p.ID).Max();

            if (!string.IsNullOrEmpty(sReturn))
            {
                int thutu = int.Parse(sReturn.Substring(8, 3));
                thutu = thutu + 1;
                if (thutu < 10)
                {
                    sReturn = sToday + "00" + thutu.ToString();
                }
                else if (thutu < 100)
                {
                    sReturn = sToday + "0" + thutu.ToString();
                }
                else if (thutu < 1000)
                {
                    sReturn = sToday + thutu.ToString();
                }
            }
            else
            {
                sReturn = sToday + "001";
            }

            return sReturn;
        }
        public SELL Get(string id)
        {
            return _db.SELLs.Where(p => p.ID.Equals(id) && p.ISHOW.Equals(true)).SingleOrDefault();

        }
        public int Delele(string id)
        {

            try
            {
                var buyObject = Get(id);
                if (buyObject != null)
                {
                    buyObject.ISHOW = false;
                    _db.SubmitChanges();
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public List<SELL> GetList()
        {
            return _db.SELLs.ToList();
        }


        public List<SELL> Search(String Name)
        {
            return _db.SELLs.Where(p => p.SUBJECT.Contains(Name)).OrderBy(p => p.NGAYDANG).ToList();
        }

        public int ActiveProduct(SELL obj)
        {
            var sanpham = Get(obj.ID);
            if (sanpham != null)
            {
                sanpham.ISHOW = true;
                _db.SubmitChanges();
                return 1;
            }
            return 0;
        }
        public int DeActiveProduct(SELL obj)
        {
            var sanpham = Get(obj.ID);
            if (sanpham != null)
            {
                sanpham.ISHOW = false;
                _db.SubmitChanges();
                return 1;
            }
            return 0;
        }
    }
}
